<template>
  <el-container>
    <el-aside width="200px" v-loading="deptLoading">
      <el-container>
        <el-header>
          <el-input
              v-model="deptName"
              placeholder="请输入部门名称"
              clearable
              size="default"
              prefix-icon="el-icon-search"
          />
        </el-header>
        <el-main class="nopadding">
          <el-tree
              class="menu"
              ref="tree"
              node-key="deptId"
              default-expand-all
              highlight-current
              :current-node-key="''"
              :data="deptList"
              :props="deptProps"
              :expand-on-click-node="false"
              :filter-node-method="deptFilterNodeMethod"
              @node-click="handleDeptClick"
          />
        </el-main>
      </el-container>
    </el-aside>

    <el-container>
      <el-header>
        <div class="left-panel">
          <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              @click="handleAdd"
          >新增
          </el-button>
          <el-button
              type="success"
              plain
              icon="el-icon-edit"
              :disabled="single"
              @click="handleUpdate"
          >修改
          </el-button>
          <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              :disabled="multiple"
              @click="handleDelete"
          >删除
          </el-button>
        </div>
        <div class="right-panel">
          <jcx-table-bar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></jcx-table-bar>
        </div>
      </el-header>

      <el-main>
        <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="岗位名称" prop="postName">
                <el-input
                    v-model="queryParams.postName"
                    placeholder="请输入岗位名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="岗位状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="岗位状态" clearable>
                  <el-option key="1" label="启用" :value="true"/>
                  <el-option key="0" label="停用" :value="false"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <el-table ref="table" v-loading="loading" :data="postList" row-key="postId"
                  @selection-change="handleSelectionChange"
                  :stripe="config.stripe" :border="config.border" :size="config.size" style="width: 100%">
          <el-table-column type="selection"></el-table-column>
          <el-table-column label="岗位标识" align="center" prop="postCode" v-if="columns[0].visible"/>
          <el-table-column label="岗位名称" align="center" prop="postName" v-if="columns[1].visible"/>
          <el-table-column label="状态" align="center" prop="status" v-if="columns[2].visible">
            <template #default="props">
              <el-tag v-if="props.row.status" type="success">启用</el-tag>
              <el-tag v-if="!props.row.status" type="danger">停用</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[3].visible"></el-table-column>
          <el-table-column label="备注" align="center" prop="remark" v-if="columns[4].visible"></el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="props">
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(props.row)"
              >修改
              </el-button>
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(props.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <jcx-page
            v-show="total>0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        ></jcx-page>

        <el-dialog destroy-on-close append-to-body width="900px" :title="title" v-model="open"
                   :close-on-click-modal="false" @closed="cancel">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="岗位标识" prop="postCode">
                  <el-input v-model="form.postCode" clearable placeholder="请输入岗位标识"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="岗位名称" prop="postName">
                  <el-input v-model="form.postName" clearable placeholder="请输入岗位名称"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="是否有效" prop="status">
                  <el-switch v-model="form.status" :active-value="true" :inactive-value="false"></el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所属部门" prop="deptId">
                  <el-cascader v-model="form.deptId" :options="deptList" :props="formDeptProps"
                               :show-all-levels="false"
                               clearable style="width: 100%;" placeholder="请选择所属部门"></el-cascader>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input type="textarea" v-model="form.remark" clearable placeholder="请输入备注"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <template #footer>
            <el-button type="primary" :loading="saveing" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </template>
        </el-dialog>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Post",
  data() {
    return {
      // 部门遮罩层
      deptLoading: true,
      // 搜索用的部门名称
      deptName: '',
      // 部门列表数据
      deptList: [],
      // 部门 props
      deptProps: {
        label: (data) => {
          return data.deptName
        }
      },
      // 部门 props
      formDeptProps: {
        value: "deptId",
        label: "deptName",
        emitPath: false,
        checkStrictly: true
      },
      // 显示搜索条件
      showSearch: true,
      // 分页总条数
      total: 0,
      // 查询参数
      queryParams: {
        deptId: undefined,
        postName: undefined,
        status: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 遮罩层
      loading: true,
      // 岗位列表数据
      postList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 表格配置
      config: {
        // 表格大小
        size: "default",
        // 表格边框
        border: true,
        // 表格斑马图纹
        stripe: true
      },
      // 保存遮罩层
      saveing: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          {required: true, message: "岗位名称不能为空", trigger: "blur"}
        ],
        postCode: [
          {required: true, message: "岗位标识不能为空", trigger: "blur"}
        ]
      },
      columns: [
        {key: 0, label: `岗位标识`, visible: true},
        {key: 1, label: `岗位名称`, visible: true},
        {key: 2, label: `状态`, visible: true},
        {key: 3, label: `创建时间`, visible: false},
        {key: 4, label: `备注`, visible: true}
      ],
    };
  },
  created() {
    this.getList();
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      if (this.deptList.length === 0) {
        this.deptLoading = true;
        this.$API.dept.pageListDept.get({status: true}).then(response => {
          if (response.status) {
            this.deptList = response.data.records;
            this.deptLoading = false;
          } else {
            this.$message.warning(response.msg);
          }
        });
      }
      this.$API.post.pageListPost.get(this.queryParams).then(response => {
        if (response.status) {
          this.postList = response.data.records;
          this.total = response.data.total;
        } else {
          this.$message.warning(response.msg);
        }
        this.loading = false;
      });
    },
    /** 筛选节点 */
    deptFilterNodeMethod(value, data) {
      if (!value) return true;
      return data.deptName.indexOf(value) !== -1;
    },
    /** 节点单击事件 */
    handleDeptClick(data) {
      this.queryParams.deptId = data.deptId;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.deptId = undefined
      this.$refs["queryForm"].resetFields();
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postId = row.postId || this.ids
      this.$API.post.getPostById.get(postId).then(response => {
        if (response.status) {
          this.form = response.data;
          this.open = true;
          this.title = "修改岗位";
        } else {
          this.$message.warning(response.msg);
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId ? [row.postId] : this.ids;
      this.$confirm(`确定删除选中的 ${postIds.length} 项吗？`, '提示', {
        type: 'warning'
      }).then(() => {
        this.$API.post.delPosts.delete(postIds).then(response => {
          if (response.status) {
            this.$message.success(response.msg);
            this.getList();
          } else {
            this.$alert(response.msg, "提示", {type: 'error'});
          }
        });
      }).catch(() => {
      })
    },
    /** 表单重置 */
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        deptId: undefined,
        remark: undefined,
        status: true
      };
      this.$nextTick(() => {
        if (this.$refs['form']) {
          this.$refs['form'].clearValidate();
          this.$refs['form'].resetFields();
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.saveing = true;
          // 如果 ID 存在就是更新
          if (this.form.postId) {
            this.$API.post.editPost.put(this.form).then(response => {
              if (response.status) {
                this.$message.success(response.msg);
                this.saveing = false;
                this.open = false;
                this.getList();
              } else {
                this.$alert(response.msg, "提示", {type: 'error'});
                this.saveing = false;
              }
            });
          } else {
            // 不存在就是新增
            this.$API.post.addPost.post(this.form).then(response => {
              if (response.status) {
                this.$message.success(response.msg);
                this.saveing = false;
                this.open = false;
                this.getList();
              } else {
                this.$alert(response.msg, "提示", {type: 'error'});
                this.saveing = false;
              }
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.saveing = false;
      this.reset();
    },
  }
};
</script>
